package me.hetong.caigou.util;

import cn.hutool.core.util.StrUtil;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import me.hetong.caigou.Global;
import org.kordamp.ikonli.javafx.FontIcon;

public class FxUtils {
    public static VBox createVBox() {
        VBox vBox = new VBox();
        vBox.setSpacing(Global.config.getSpace());
        vBox.setPadding(new Insets(Global.config.getPadding()));
        return vBox;
    }

    public static HBox createHBox() {
        HBox hBox = new HBox();
        hBox.setSpacing(Global.config.getSpace());
        hBox.setAlignment(Pos.CENTER_LEFT);
        return hBox;
    }

    public static Button createButton(String name) {
        Button button = new Button(name);
        button.setFocusTraversable(false);
        return button;
    }


    public static Label createLabel(String name) {
        Label label = new Label(name);
        return label;
    }

    public static Label createLabelProIcon(String text, String iconCode, EventHandler<MouseEvent> event) {

        String defaultStyle = "-fx-background-color:#22222266;-fx-padding:4 8;-fx-background-radius: 2;-fx-text-fill: #eeeeee";
        String hoverStyle = "-fx-background-color:#9878ef99;-fx-padding: 4 8;-fx-background-radius: 2;-fx-text-fill: #eeeeee";

        Label label = new Label(text);
        label.setStyle(defaultStyle);

//        FontIcon fontIcon = new FontIcon(iconCode);
//        fontIcon.setFill(Color.WHITE);
//        fontIcon.setIconSize(18);
//        label.setGraphic(fontIcon);
//        label.setFont(Font.font(10));

//        int prefWidth = 30;
//        label.setPrefSize(prefWidth, prefWidth);
//        label.setMinSize(prefWidth, prefWidth);
//        label.setMaxSize(prefWidth, prefWidth);
//        label.minWidth(30);
//        label.prefWidth(30);
//        label.maxWidth(30);
        label.setAlignment(Pos.CENTER);

//        if (StrUtil.isNotBlank(path)) {
//            ImageView value = new ImageView(path);
//            value.setFitWidth(20);
//            value.setFitHeight(20);
//            label.setGraphic(value);
//        }

        if (event != null) {
            label.setOnMouseClicked(event);
        }

        label.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                label.setStyle(hoverStyle);
            }
        });
        label.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                label.setStyle(defaultStyle);
            }
        });

        return label;
    }


    public static Label createLabelPro(String text, EventHandler<MouseEvent> event) {

        String defaultStyle = "-fx-background-color:#ffffff55;-fx-padding:4 8;-fx-background-radius: 2;-fx-text-fill: #eeeeee";
        String hoverStyle = "-fx-background-color:#9878ef99;-fx-padding:4 8;-fx-background-radius: 2;-fx-text-fill: #eeeeee";

        Label label = new Label(text);
        label.setStyle(defaultStyle);

        if (event != null) {
            label.setOnMouseClicked(event);
        }

        label.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                label.setStyle(hoverStyle);
            }
        });
        label.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                label.setStyle(defaultStyle);
            }
        });

        return label;
    }

    public static TextField createTextField(String text) {
        TextField textField = new TextField(text);
        textField.setFocusTraversable(false);
        return textField;
    }

    public static TextArea createTextArea(String text) {
        TextArea textArea = new TextArea(text);
        return textArea;
    }

    public static CheckMenuItem createCheckMenuItem(String text) {
        CheckMenuItem checkMenuItem = new CheckMenuItem();

        if (StrUtil.isNotBlank(text)) {
            checkMenuItem.setText(text);
        }

        return checkMenuItem;
    }

    public static ToggleButton createToggleButton(String text, String tips, String icon) {
        ToggleButton toggleButton = new ToggleButton();
        toggleButton.setFocusTraversable(false);

        toggleButton.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                toggleButton.setCursor(Cursor.HAND);
            }
        });

        if (StrUtil.isNotBlank(text)) {
            toggleButton.setText(text);
        }

        if (StrUtil.isNotBlank(tips)) {
            toggleButton.setTooltip(new Tooltip(tips));
        }

        if (StrUtil.isNotBlank(icon)) {
            ImageView value = new ImageView(icon);
            value.setFitWidth(20);
            value.setFitHeight(20);
            toggleButton.setGraphic(value);
        }
        return toggleButton;
    }
}
